#include "kernel/param.h"
#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
#include "kernel/fs.h"
#include "kernel/fcntl.h"
#include "kernel/syscall.h"
//#include "kernel/traps.h"
#include "kernel/memlayout.h"

int
main(int argc, char *argv[]) {
  int fp;
  char *buffer = 0;
  uint len;
  if (argc != 5) {
    printf( "usage: ./lseektest <filename> <offset> <len> <string>\n");
    exit(1);
  }

  if ((fp = open(argv[1], O_RDONLY)) < 0) {
    printf( "unable to open file %s\n", argv[1]);
    exit(1);
  }
  
  len = atoi(argv[3]);
  if ((buffer = (char *)malloc(len + 1)) < 0) {
    printf( "unable to allocate buffer\n");
    exit(1);
  }

  int offset = atoi(argv[2]);
  int ret;
  ret = lseek(fp, SEEK_SET, offset);
  if (ret < 0) {
    printf( "unable to lseek\n");
    exit(1);
  }

  read(fp, buffer, len);
  buffer[len] = 0;

  printf( "(%s:%s)\n", argv[4], buffer);

  if (strcmp(buffer, argv[4])) {
    printf( "strings do not match\n");
    exit(1);
  }

  printf( "strings match\n");

  exit(0);
}



